﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AIC_CD
{
    public partial class BaoCaoThongKeSoLuongChatLuongCanBoCongDoan_Update1 : MyFromBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblNotice.Visible = false;

            CheckPermission();
            if (!IsPostBack)
            {
                Config();
                BindDataToControls();
            }
        }

        /// <summary>
        /// Thủ tục kiểm tra quyền sử dụng của người dùng có được quyền truy cập vào mục này hay không?
        /// </summary>
        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("ThongKeSoLuongChatLuongCanBoCongDoan");
            if (Role == null || Role["Xem"] == 0 || ((Role["Xem"] == 1 && Role["Sua"] == 0)))
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }

        /// <summary>
        /// Thủ tục cấu hình giao diện, các controls
        /// </summary>
        private void Config()
        {
            //TODO
            txtNguoiLapBaoCao.Text = "";
            txtThuTruongCoQuanChinhTri.Text = "";

        }

        /// <summary>
        /// Bind Data vào các contrls như Combobox, ListBox, textBox ...
        /// </summary>
        private void BindDataToControls()
        {
            //TODO
            if (Session["userid"] != null)
            {

                string sql = "Select DV.* " +
                             "From tblDonVi DV " +
                             "Inner Join tblUser U On U.DonViID=DV.ID " +
                             "Where U.ID=" + Session["userid"];
                DataTable dtDV = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
                tdTenDonVi.InnerText = "(" + dtDV.Rows[0]["MaDV"] + ") " + dtDV.Rows[0]["TenDV"];

                sql = "Select ID,TenDM From tblDanhMuc Where MaDM='" + MyConst.DanhMuc.KyBaoCao.Ma + "' ";
                SqlDataReader reKyBaoCao = SqlHelper.ExecuteReader(CommandType.Text, sql);
                clsDataBind.CreateCombo("Tất cả", reKyBaoCao, cboKyBaoCao, 1, 0);
            }
        }

        /// <summary>
        /// Bind Data vào gridview
        /// </summary>
        private void BindDataToGrid()
        {
            bool IsEmpty = false;
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            arrParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));

            string sql = "SELECT * FROM dbo.tblDanhSachSoLuongChatLuongCanBoCongDoanCapDauMoiTrucThuocBoQuocPhong WHERE DonViID = @DonViID AND KyBaoCaoID_DM = @KyBaoCaoID_DM";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            if (dt.Rows.Count == 0)
            {
                IsEmpty = true;
                DataRow dr = dt.NewRow();
                dr["F1"] = "";
                dr["F2"] = "";
                dr["F3"] = "";
                dr["F4"] = "";
                dr["F5"] = "";
                dr["F6"] = "";
                dr["F7"] = "";
                dr["F8"] = "";
                dr["F9"] = "";
                dr["F10"] = "";
                dr["F11"] = "";
                dr["F12"] = "";
                dr["F13"] = "";
                dr["F14"] = "";
                dr["F15"] = "";
                dt.Rows.InsertAt(dr, 0);

            }
            else
            {
                BindDataToTextField();
            }
            grvData.ShowFooter = true;
            grvData.DataSource = dt;
            grvData.DataBind();
            if (IsEmpty)
            {
                grvData.Rows[0].Visible = false;
            }
        }

        #region Events

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void cboKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboKyBaoCao.SelectedIndex > 0)
            {
                btnLuu.Visible = true;
                grvData.Visible = true;
                grvData.EditIndex = -1;
                BindDataToGrid();
            }
            else
            {
                btnLuu.Visible = false;
                grvData.Visible = false;
            }
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {

            String sql = "SELECT * FROM dbo.tblDanhSachSoLuongChatLuongCanBoCongDoanCapDauMoiTrucThuocBoQuocPhong WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND IsCommon = 1";
            List<SqlParameter> lstParameters = new List<SqlParameter>();
            lstParameters.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParameters.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));

            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParameters);
            if (dt.Rows.Count == 0)
            {
                btnLuu.CommandName = MyConst.Action.Addnew;
            }
            else
            {
                btnLuu.CommandName = MyConst.Action.Update;
            }

            if (btnLuu.CommandName == MyConst.Action.Addnew)
            {
                string sSQL = "INSERT INTO dbo.tblDanhSachSoLuongChatLuongCanBoCongDoanCapDauMoiTrucThuocBoQuocPhong" +
                              "(KyBaoCaoID_DM ,DonViID ,CM_NguoiTongHop ,CM_ThuTruong ,CM_TenBaoCao ,CM_ThoiGianBaoCao" +
                              ",IsCommon ,CreateDate ,UpdateDate ,UserID)" +
                              "VALUES  ( @KyBaoCaoID_DM ,@DonViID ,@CM_NguoiTongHop ,@CM_ThuTruong ,@CM_TenBaoCao ," +
                              "GETDATE() ,1 ,GETDATE() ,GETDATE() ,@UserID)";

                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
                lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_NguoiTongHop", SqlDbType.NVarChar, ParameterDirection.Input, txtNguoiLapBaoCao.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThuTruong", SqlDbType.NVarChar, ParameterDirection.Input, txtThuTruongCoQuanChinhTri.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(Session["userid"])));

                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                lblNotice.Visible = true;
                lblNotice.InnerHtml = "<font color='Green'>Lưu báo cáo thành công</font>";
                MyCommon.AddMessage(this.Page.Master, "Lưu thông tin báo cáo thanh công", MyConst.MessageType.Success);
            }
            else
            {
                string sSQL = "Update tblDanhSachSoLuongChatLuongCanBoCongDoanCapDauMoiTrucThuocBoQuocPhong Set CM_NguoiTongHop=@CM_NguoiTongHop, CM_ThuTruong=@CM_ThuTruong, CM_TenBaoCao=@CM_TenBaoCao " +
                                "Where IsCommon=1 And DonViID=" + MyCommon.GetDonViID(Session["userid"].ToString()) + " And KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;

                List<SqlParameter> lstParameter = new List<SqlParameter>();
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_NguoiTongHop", SqlDbType.NVarChar, ParameterDirection.Input, txtNguoiLapBaoCao.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThuTruong", SqlDbType.NVarChar, ParameterDirection.Input, txtThuTruongCoQuanChinhTri.Text.Trim()));
                lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim()));


                SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);

                lblNotice.Visible = true;
                lblNotice.InnerHtml = "<font color='Green'>Lưu báo cáo thành công</font>";
                MyCommon.AddMessage(this.Page.Master, "Lưu thông tin báo cáo thanh công", MyConst.MessageType.Success);
            }

        }

        #endregion Events


        #region GridCommand

        private static void AddMergedCells(GridViewRow objgridviewrow, TableCell objtablecell, int colspan, int rowspan, string celltext)
        {
            objtablecell = new TableCell();
            objtablecell.Text = celltext;
            objtablecell.ColumnSpan = colspan;
            objtablecell.RowSpan = rowspan;
            objtablecell.HorizontalAlign = HorizontalAlign.Center;
            objgridviewrow.Cells.Add(objtablecell);
        }

        protected void BindDataToTextField()
        {
            string sql = "SELECT * FROM dbo.tblDanhSachSoLuongChatLuongCanBoCongDoanCapDauMoiTrucThuocBoQuocPhong WHERE KyBaoCaoID_DM = @KyBaoCaoID_DM AND DonViID = @DonViID AND IsCommon = 1";
            List<SqlParameter> lstParameters = new List<SqlParameter>();
            lstParameters.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
            lstParameters.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, lstParameters);
            if (dt.Rows.Count != 0)
            {
                txtTenBaoCao.Text = dt.Rows[0]["CM_TenBaoCao"].ToString();
                txtNguoiLapBaoCao.Text = dt.Rows[0]["CM_NguoiTongHop"].ToString();
                txtThuTruongCoQuanChinhTri.Text = dt.Rows[0]["CM_ThuTruong"].ToString();
            }
        }

        protected void grvData_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow3 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "STT");


                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Họ và tên Quê quán Trú quán");

                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Năm sinh");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "N.ngũ T.dụng");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Cấp bậc (SQ,QNCN,CNV...)");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "Trình độ");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "Chúc vụ");
                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "Qua đào tạo Công đoàn");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Nam, Nữ, ");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Dân tộc");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Thời gian về đơn vị, cơ quan");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Ghi chú");

                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "Sửa");
                objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);

                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "C.môn (Qua trường)");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "C.trị (SC,TC,Cao cấp)");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Đảng");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Chính quyền");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "ĐH C.đoàn");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Bồi dưỡng N.vụ");

                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "1");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "2");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "3");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "4");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "5");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "6");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "7");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "8");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "9");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "10");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "11");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "12");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "13");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "14");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "15");

                objgridviewrow3.BackColor = System.Drawing.Color.FromArgb(0xC7, 0xF4, 0x9C);
                objGridView.Controls[0].Controls.AddAt(2, objgridviewrow3);
            }
        }

        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {


            RowDataBound(sender, e);
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;


            if ((sCommandName == AIC_CD.MyConst.Action.Approve))
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {

                    // cap nhat vao database
                    TextBox txtHoTenEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtHoTenEdit");
                    TextBox txtNamSinhEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtNamSinhEdit");
                    TextBox txtNNguEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtNNguEdit");
                    TextBox txtCBacEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtCBacEdit");
                    TextBox txtCMonEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtCMonEdit");
                    TextBox txtCTriEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtCTriEdit");
                    TextBox txtDangEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtDangEdit");
                    TextBox txtCQuyenEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtCQuyenEdit");
                    TextBox txtDHDoanEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtDHDoanEdit");
                    TextBox txtBDuongEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtBDuongEdit");
                    TextBox txtNamNuEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtNamNuEdit");
                    TextBox txtDanTocEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtDanTocEdit");
                    TextBox txtVeDonViEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtVeDonViEdit");
                    TextBox txtGhiChuEdit = (TextBox)grvData.Rows[grvData.EditIndex].FindControl("txtGhiChuEdit");

                    string sSQL = "UPDATE dbo.tblDanhSachSoLuongChatLuongCanBoCongDoanCapDauMoiTrucThuocBoQuocPhong SET " +
                                  "F2 = @F2,F3 = @F3,F4 = @F4,F5 = @F5,F6 = @F6,F7 = @F7,F8 = @F8,F9 = @F9,F10 = @F10," +
                                  "F11 = @F11,F12 = @F12,F13 = @F13,F14 = @F14,F15 = @F15,UpdateDate = GETDATE(),UserID = @UserID WHERE ID = @ID";

                    List<SqlParameter> lstParameter = new List<SqlParameter>();
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.NVarChar, ParameterDirection.Input, txtHoTenEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.NVarChar, ParameterDirection.Input, txtNamSinhEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.NVarChar, ParameterDirection.Input, txtNNguEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.NVarChar, ParameterDirection.Input, txtCBacEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.NVarChar, ParameterDirection.Input, txtCMonEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.NVarChar, ParameterDirection.Input, txtCTriEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.NVarChar, ParameterDirection.Input, txtDangEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.NVarChar, ParameterDirection.Input, txtCQuyenEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F10", SqlDbType.NVarChar, ParameterDirection.Input, txtDHDoanEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F11", SqlDbType.NVarChar, ParameterDirection.Input, txtBDuongEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F12", SqlDbType.NVarChar, ParameterDirection.Input, txtNamNuEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F13", SqlDbType.NVarChar, ParameterDirection.Input, txtDanTocEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F14", SqlDbType.NVarChar, ParameterDirection.Input, txtVeDonViEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F15", SqlDbType.NVarChar, ParameterDirection.Input, txtGhiChuEdit.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));

                    lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));

                    SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                    MyTrans.Commit();
                    grvData.EditIndex = -1;
                    BindDataToGrid();
                    lblNotice.Visible = true;
                    lblNotice.InnerText = "Cập nhật thành công";
                    MyCommon.AddMessage(this.Master, "Cập nhật thành công !", MyConst.MessageType.Success);
                }
                catch (Exception)
                {
                    MyTrans.Rollback();
                }

            }
            else if (e.CommandName == AIC_CD.MyConst.Action.Addnew)
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {
                    TextBox txtHoTenNew = (TextBox)grvData.FooterRow.FindControl("txtHoTenNew");
                    TextBox txtNamSinhNew = (TextBox)grvData.FooterRow.FindControl("txtNamSinhNew");
                    TextBox txtNNguNew = (TextBox)grvData.FooterRow.FindControl("txtNNguNew");
                    TextBox txtCBacNew = (TextBox)grvData.FooterRow.FindControl("txtCBacNew");
                    TextBox txtCMonNew = (TextBox)grvData.FooterRow.FindControl("txtCMonNew");
                    TextBox txtCTriNew = (TextBox)grvData.FooterRow.FindControl("txtCTriNew");
                    TextBox txtDangNew = (TextBox)grvData.FooterRow.FindControl("txtDangNew");
                    TextBox txtCQuyenNew = (TextBox)grvData.FooterRow.FindControl("txtCQuyenNew");
                    TextBox txtDHoiDoanNew = (TextBox)grvData.FooterRow.FindControl("txtDHoiDoanNew");
                    TextBox txtBDuongNew = (TextBox)grvData.FooterRow.FindControl("txtBDuongNew");
                    TextBox txtNamNuNew = (TextBox)grvData.FooterRow.FindControl("txtNamNuNew");
                    TextBox txtDanTocNew = (TextBox)grvData.FooterRow.FindControl("txtDanTocNew");
                    TextBox txtVeDonViNew = (TextBox)grvData.FooterRow.FindControl("txtVeDonViNew");
                    TextBox txtGhiChuNew = (TextBox)grvData.FooterRow.FindControl("txtGhiChuNew");

                    string sql = "INSERT INTO dbo.tblDanhSachSoLuongChatLuongCanBoCongDoanCapDauMoiTrucThuocBoQuocPhong" +
                                 "(KyBaoCaoID_DM,DonViID,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,CreateDate,UpdateDate,UserID)" +
                                 "VALUES(@KyBaoCaoID_DM,@DonViID,@F2,@F3,@F4,@F5,@F6,@F7,@F8,@F9,@F10,@F11,@F12,@F13,@F14,@F15,GETDATE(),GETDATE(),@UserID)";
                    List<SqlParameter> lstParameter = new List<SqlParameter>();
                    lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(cboKyBaoCao.SelectedValue)));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, MyCommon.GetDonViID(Session["userid"].ToString())));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.NVarChar, ParameterDirection.Input, txtHoTenNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.NVarChar, ParameterDirection.Input, txtNamSinhNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.NVarChar, ParameterDirection.Input, txtNNguNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.NVarChar, ParameterDirection.Input, txtCBacNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.NVarChar, ParameterDirection.Input, txtCMonNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.NVarChar, ParameterDirection.Input, txtCTriNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.NVarChar, ParameterDirection.Input, txtDangNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.NVarChar, ParameterDirection.Input, txtCQuyenNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F10", SqlDbType.NVarChar, ParameterDirection.Input, txtDHoiDoanNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F11", SqlDbType.NVarChar, ParameterDirection.Input, txtBDuongNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F12", SqlDbType.NVarChar, ParameterDirection.Input, txtNamNuNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F13", SqlDbType.NVarChar, ParameterDirection.Input, txtDanTocNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F14", SqlDbType.NVarChar, ParameterDirection.Input, txtVeDonViNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F15", SqlDbType.NVarChar, ParameterDirection.Input, txtGhiChuNew.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Convert.ToInt32(Session["userid"].ToString())));

                    int Result = SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                    if (Result == 0)
                    {
                        lblNotice.Visible = true;
                        lblNotice.InnerText = "Có lỗi xảy ra trong quá trình thêm mới";
                        MyCommon.AddMessage(this.Master, "Có lỗi xảy ra trong quá trình thêm mới", MyConst.MessageType.Error);
                    }
                    else
                    {
                        lblNotice.Visible = true;
                        lblNotice.InnerHtml = "<font color='green'>Thêm mới thành công !</font>";
                        MyCommon.AddMessage(this.Master, "Thêm mới thành công !", MyConst.MessageType.Success);
                    }

                    MyTrans.Commit();
                    BindDataToGrid();
                }
                catch (Exception)
                {
                    MyTrans.Rollback();
                }
            }
            else if ((sCommandName == AIC_CD.MyConst.Action.Delete))
            {
                string sql = "DELETE FROM dbo.tblDanhSachSoLuongChatLuongCanBoCongDoanCapDauMoiTrucThuocBoQuocPhong WHERE ID = @ID";
                List<SqlParameter> lstParameter = new List<SqlParameter>();

                lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);

                grvData.EditIndex = -1;
                BindDataToGrid();
                lblNotice.Visible = true;
                lblNotice.InnerText = "Xóa thành công";
                MyCommon.AddMessage(this.Master, "Xóa thành công !", MyConst.MessageType.Success);
            }
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvData.EditIndex = e.NewEditIndex;
            BindDataToGrid();
        }



        protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //grvData.PageIndex = e.NewPageIndex;
            //BindDataToGrid();
        }


        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            BindDataToGrid();
        }

        #endregion GridCommand

        protected void grvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            grvData.DeleteRow(e.RowIndex);
            grvData.DataBind();
        }

    }
}
